package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.LossStudent;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.query.StudentQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IStudentManagementService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("studentmanagement")
public class Studentmanagement {
    @Autowired
    private IStudentManagementService studentManagementService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("studentView")
    public String studentView(){
        return "studentmanagement/student";
    }

    @RequestMapping("studentLossView")
    public String studentLossView(){
        return "studentmanagement/studentLoss";
    }


    @RequestMapping("StudentList")
    @ResponseBody//查询所有正式学员
    public PageResult StudentList(StudentQuery q){
        PageResult pageResult=studentManagementService.getPageResult(q);
            return pageResult;
    }
    @ResponseBody
    @RequestMapping("updateState")//休学状态
    public Object updateState(Long id,String state){
        JSONResult jsonResult=new JSONResult();
        try {
            studentManagementService.updateState(id,state);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark(e.getMessage());
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("updateClass")//转班
    public Object updateClass(Long id,String newClassName,String state){
        JSONResult jsonResult=new JSONResult();
        try {
            studentManagementService.updateClass(id,newClassName,state);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark(e.getMessage());
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("update")//编辑正式学员
    public Object update(PotentialStudent student){
        JSONResult jsonResult=new JSONResult();
        try {
            studentManagementService.update(student);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark(e.getMessage());
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("updateLossState")//正式学员改变状态为流失状态
    public Object updateLossState(LossStudent s,String username){
        JSONResult jsonResult=new JSONResult();
        try {
            Employee employee = employeeService.selectByUsername(username);
            s.setSalesman(employee);
            studentManagementService.insertLoss(s);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark("操作失败");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("lossList")//查询所有流失学员
    public PageResult lossList(StudentQuery q){
        PageResult pageResult=studentManagementService.getLossPageResult(q);
        return pageResult;
    }
    @ResponseBody
    @RequestMapping("audit")//审核
    public Object audit(Long id){
        JSONResult jsonResult=new JSONResult();
        try {
            studentManagementService.auditLoss(id);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark("审核失败");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("updateLoss")//编辑
    public Object updateLoss(LossStudent s){
        JSONResult jsonResult=new JSONResult();
        try {
            studentManagementService.updateLoss(s);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark("操作失败");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("reverse")//审核反转
    public Object reverse(Long id){
        JSONResult jsonResult=new JSONResult();
        try {
           studentManagementService.reverseState(id);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.remark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("exportXLS")//导出
    public void exportBack(HttpServletResponse response) throws IOException {
       /* response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook wk=new HSSFWorkbook();
        Sheet seet=wk.createSheet("day01");
        List<Employee>list=employeeService.selectAll();
        Row row=seet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("密码");
        row.createCell(3).setCellValue("电话");
        row.createCell(4).setCellValue("邮箱");
        row.createCell(5).setCellValue("部门");
        row.createCell(6).setCellValue("入职时间");
        row.createCell(7).setCellValue("状态");
        row.createCell(8).setCellValue("是否管理员");
        for(int i=0;i<list.size();i++){
            Employee e=list.get(i);
            row=seet.createRow(i+1);
            row.createCell(0).setCellValue(e.getUsername());
            row.createCell(1).setCellValue(e.getRealname());
            row.createCell(2).setCellValue(e.getPassword());
            row.createCell(3).setCellValue(e.getTel());
            row.createCell(4).setCellValue(e.getEmail());
            row.createCell(5).setCellValue(e.getDept().getName());
            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
            row.createCell(6).setCellValue(formater.format(e.getHireDate()));
            if(e.getState()){
                row.createCell(7).setCellValue("在职");
            }else{
                row.createCell(7).setCellValue("离职");
            }
            if(e.getAdmin()){
                row.createCell(8).setCellValue("是");
            }else{
                row.createCell(8).setCellValue("否");
            }

        }
        wk.write(response.getOutputStream());*/
    }
}
